package interfaces.impl;

import java.util.ArrayList;

import objects.WorldObject;
import universe.Universe;
import constants.Constants;
import extra.ObjectId;
import extra.Position;

public abstract class HumanInteraction extends Agent{
	
	//	protected ArrayList<Position> parametersList;
	protected ArrayList<Object> listParameter = new ArrayList<Object>();
	protected String parameters=null;
	protected boolean isAvalible=true;
	protected int permisology = Constants.NO_PERMISOLOGY;
	
	public HumanInteraction (){
		super();
		initializeParametersList();
		initializeActionList();
		generateMsgParameter();
		isAvalible=true;
		isActive=false;
	}
	
	private void generateMsgParameter() {
		parameters=new String();
		for(Object obj : listParameter){
			if (obj instanceof ObjectId) {
				
				parameters=parameters+Constants.PARAMETER_OBJECT+";";
				
			}
			if (obj instanceof Position) {
				parameters=parameters+Constants.PARAMETER_POSITION+";";
			}
		}
		
	}

	public void addParameter(Object parameter){
		if( parameter instanceof ObjectId || parameter instanceof Position){
			listParameter.add(parameter);
		}
	}
	
	public boolean setParameter(String parameter,Universe universe){
		if(!isAvalible){
			System.out.println("No eestoy dipobible "+name);
			return false;
		}
		for(Object obj : listParameter){
			if (obj instanceof ObjectId) {
				ObjectId objectId = (ObjectId) obj;
				getObjectParameter(parameter, objectId, universe);
				
				
			}
			if (obj instanceof Position) {
				Position position = (Position) obj;
				parameter=getPositionParameter(parameter, position);
				
			}
		}
		System.out.println("ME ACTIVE: "+name);
		isActive=true;
		return true;
	}
	
	
	public  String getParameters(){
		return parameters;
	}
	
	public int getTotalParameters(){
		if(parameters==null){
			return 0;
		}
		if(parameters.isEmpty()){
		return 0;
		}
		return 1;
	}
	
	public int getPermisology() {
		return permisology;
	}

	public void setPermisology(int permisology) {
		this.permisology = permisology;
	}

	
	public String getObjectParameter(String parameters, ObjectId object ,Universe universe){
		
		
		String str[] = parameters.split("/");
		object.setObjectId(Integer.parseInt(str[0])); 
		
		
		
		//System.out.println("MI ID12 "+object); 
		
		parameters=new String();
		for(int i= 1;i<str.length;i++){
			parameters=parameters+str[i]+"/";
		}
		
		return parameters;
	}
	
	
	public String getPositionParameter(String parameters,Position pos){
		
		
		String str[] = parameters.split("/");
		String position[] = str[0].split(";");
		
		int x = Integer.parseInt(position[0]);
		int y = Integer.parseInt(position[1]);
		
		pos.setX(x);
		pos.setY(y);
		
		//System.out.println("esto es en human interaccion obteniendo posicion: "+str[0]+" "+pos.getX()+" "+pos.getY());

		parameters=new String();
		for(int i= 1;i<str.length;i++){
			parameters=parameters+str[i]+"/";
		}
		
		return parameters;
	}

	public boolean isAvalible() {
		return isAvalible;
	}

	public void setAvalible(boolean isAvalible) {
		this.isAvalible = isAvalible;
	}

	public void update(WorldObject object, Universe universe, double time){
		if (!isActive){
			return;
		}
		if(!isAvalible){
			return;
		}
		if(!canUpdate(object)){
			System.out.println("Me falta alguna accion para poder actuar, soy el agent: "+name);
			return;
		}
		if(!canUpdateSpecialFeature(object)){
			System.out.println("Me falta algun special feature, soy el agent: "+name);
			return;
		}
		
		updateChild( object,  universe, time);
		
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	@Override
	public void setName(String name) {
		this.name=name;
		
	}

	public abstract void initializeParametersList();
	

}
